import clone from 'clone';

/**
 * 工作流调试、体验端调试、用户端对话中的消息公共逻辑
 * 1.多气泡的拆分逻辑
 */
const msgCodeHeader = {
    data () {
        return {
        };
    },
    methods: {
        // 针对多气泡时，修改显示规则
        changeAgentData (el) {
            const agent_thought = el.agent_thought;
            if (agent_thought && agent_thought.procedures && agent_thought.procedures.length) {
                el.agentThoughts = clone(agent_thought); // 克隆一份数据，重新定制显示规则
                const procedures = el.agentThoughts.procedures;

                const realOutputs = el.work_flow.outputs; // 后台返回的真实回复
                const outputs = [...realOutputs];
                procedures.forEach((item, index) => {
                    const emptyPlaceholder = el.is_final ? '无消息输出' : '<br/>';
                    // 没有就先给个空数据, 生成一个空气泡占位
                    outputs[item.reply_index] = realOutputs[item.reply_index] || emptyPlaceholder;
                });
                const newAgentThoughts = [];
                outputs.forEach((output, i) => {
                    newAgentThoughts[i] = {
                        ...el.agentThoughts,
                        procedures: procedures.filter((item) => item.reply_index === i)
                    };
                });

                // 按照回复，归类 thought
                el.newAgentThoughts = newAgentThoughts;
                el.workflowOutputs = outputs;
            } else {
                el.newAgentThoughts = [];
                el.workflowOutputs = el.work_flow.outputs;
            }
        },
        changeProcedureAgentIconVisible (el) {
            const agent_thought = el.agent_thought;
            if (agent_thought && agent_thought.procedures && agent_thought.procedures.length) {
                const procedureList = agent_thought.procedures;
                procedureList.forEach((procedure, idx) => {
                    const agentName = procedure.target_agent_name;
                    for (let i = 0; i < idx; i++) {
                        if (!procedureList[i].target_agent_name) {
                            // 过滤tool
                            continue;
                        }
                        if (procedureList[i].target_agent_name === agentName) {
                            // 前面存在相同的agent name，则不展示agent 的头像
                            procedure.hidden_agent_icon = true;
                            break;
                        }
                    }
                });
            }
        }
    }
};

export default msgCodeHeader;